package com.nage.components.movement;

import com.nage.components.Component;
import com.nage.components.display.Shape;
import com.nage.engine.GameObject;
import com.nage.graphics.RenderSystem;

public class OnScreen extends Component {
	// Adds constraints to the position of the GameObject that ensures it always remains 
	// on screen.

	Shape shape;
	Physics physics;
	private boolean ready;
	
	public OnScreen(GameObject p) {
		super(p);
		type = ON_SCREEN;
	}
	
	@Override
	public boolean findLinks() {
		physics = (Physics) parent.getComponent(PHYSICS);
		shape = (Shape) parent.getComponent(SHAPE);
		if(physics==null || shape==null)
			return false;
		return true;
	}

	@Override
	public void update(long delta) {
		
		if(!ready) {
			if(RenderSystem.view[2]!=0) {
				ready = true; 
			} else {
				return;
			}
		}
		
		
		if(parent.x<0 || parent.x > RenderSystem.view[2] - shape.width) {
			physics.addImpulse(physics.vel.x * -30, 0);
		} 
		
		if(parent.y<0 || parent.y > RenderSystem.view[3] - shape.height) {
			physics.addImpulse(0, physics.vel.y * -30);
		}

	}

}
